home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Resources / Sound / AHI / Developer / examples / Low-level / DoubleBuffer / DoubleBuffer.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  8KB  |  239 lines

  1. /* Simple sample player for AHI using the low-level API
  2.    with double buffered AHIST_DYNAMICSAMPLE sounds.
  3.  
  4.    Usage: DoubleBuffer < [raw sample file]
  5.  
  6.    the file must be in mono 16 bit signed big endian format sampled
  7.    in 17640 Hz.
  8.  
  9.    This software is Public Domain. */
  10.  
  11. #include <devices/ahi.h>
  12. #include <exec/exec.h>
  13. #include <proto/ahi.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16.  
  17. #define EQ ==
  18. #define MINBUFFLEN 10000
  19.  
  20. struct Library    *AHIBase;
  21. struct MsgPort    *AHImp=NULL;
  22. struct AHIRequest *AHIio=NULL;
  23. BYTE               AHIDevice=-1;
  24.  
  25. BYTE signal=-1;
  26.  
  27. struct AHIAudioModeRequester *req=NULL;
  28. struct AHIAudioCtrl *actrl=NULL;
  29.  
  30. BOOL DBflag=FALSE;  // double buffer flag
  31. ULONG BufferLen=NULL;
  32.  
  33. struct AHISampleInfo Sample0 =
  34. {
  35.   AHIST_M16S,
  36.   NULL,
  37.   NULL,
  38. };
  39.  
  40. struct AHISampleInfo Sample1 =
  41. {
  42.   AHIST_M16S,
  43.   NULL,
  44.   NULL,
  45. };
  46.  
  47. __asm __saveds ULONG SoundFunc(register __a0 struct Hook *hook,
  48.     register __a2 struct AHIAudioCtrl *actrl,
  49.     register __a1 struct AHISoundMessage *smsg)
  50. {
  51.   if(DBflag = !DBflag) // Flip and test
  52.     AHI_SetSound(0,1,0,0,actrl,NULL);
  53.   else
  54.     AHI_SetSound(0,0,0,0,actrl,NULL);
  55.   Signal(actrl->ahiac_UserData,(1L<<signal));
  56.   return NULL;
  57. }
  58.  
  59. struct Hook SoundHook =
  60. {
  61.   0,0,
  62.   SoundFunc,
  63.   NULL,
  64.   NULL,
  65. };
  66.  
  67.  
  68. int main()
  69. {
  70.   ULONG mixfreq,playsamples,readsamples,signals,i;
  71.  
  72.   if(AHImp=CreateMsgPort())
  73.   {
  74.     if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest)))
  75.     {
  76.       AHIio->ahir_Version = 4;  // Open at least version 4 of 'ahi.device'.
  77.       if(!(AHIDevice=OpenDevice(AHINAME,AHI_NO_UNIT,(struct IORequest *)AHIio,NULL)))
  78.       {
  79.         AHIBase=(struct Library *)AHIio->ahir_Std.io_Device;
  80.  
  81.         if(req=AHI_AllocAudioRequest(
  82.             AHIR_PubScreenName,"",
  83.             AHIR_TitleText,"Select a mode and rate",
  84.             AHIR_InitialMixFreq,17640,
  85.             AHIR_DoMixFreq,TRUE,
  86.             TAG_DONE))
  87.         {
  88.           if(AHI_AudioRequest(req,TAG_DONE))
  89.           {
  90.             if(actrl=AHI_AllocAudio(
  91.                 AHIA_AudioID,req->ahiam_AudioID,
  92.                 AHIA_MixFreq,req->ahiam_MixFreq,
  93.                 AHIA_Channels,1,
  94.                 AHIA_Sounds,2,
  95.                 AHIA_SoundFunc,&SoundHook,
  96.                 AHIA_UserData,FindTask(NULL),
  97.                 TAG_DONE))
  98.             {
  99.               AHI_GetAudioAttrs(AHI_INVALID_ID,actrl,
  100.                   AHIDB_MaxPlaySamples,&playsamples,
  101.                   TAG_DONE);
  102.               AHI_ControlAudio(actrl,
  103.                   AHIC_MixFreq_Query,&mixfreq,
  104.                   TAG_DONE);
  105.               BufferLen=playsamples*17640/mixfreq;
  106.               if (BufferLen<MINBUFFLEN)
  107.                 BufferLen=MINBUFFLEN;
  108.  
  109.               Sample0.ahisi_Length=BufferLen;
  110.               Sample1.ahisi_Length=BufferLen;
  111.               Sample0.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
  112.               Sample1.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
  113.  
  114.               if(Sample0.ahisi_Address && Sample1.ahisi_Address)
  115.               {
  116.                 if((!(AHI_LoadSound(0,AHIST_DYNAMICSAMPLE,&Sample0,actrl))) &&
  117.                    (!(AHI_LoadSound(1,AHIST_DYNAMICSAMPLE,&Sample1,actrl))))
  118.                 {
  119.                   if((signal=AllocSignal(-1)) != -1)
  120.                   {
  121.                     if(!(AHI_ControlAudio(actrl,
  122.                         AHIC_Play,TRUE,
  123.                         TAG_DONE)))
  124.                     {
  125. // Everything is set up now. Let's load the buffers and start rockin'...
  126.                       Read(Input(),Sample0.ahisi_Address,BufferLen*2);
  127.  
  128. // The new AHI_PlayA() function is demonstrated...
  129.                       AHI_Play(actrl,
  130.                         AHIP_BeginChannel,0,
  131.                         AHIP_Freq,17640,
  132.                         AHIP_Vol,0x10000L,
  133.                         AHIP_Pan,0x8000L,
  134.                         AHIP_Sound,0,
  135.                         AHIP_Offset,0,
  136.                         AHIP_Length,0,
  137.                         AHIP_EndChannel,NULL,
  138.                         TAG_DONE);
  139. /* These functions were available in V2, too.
  140.                       AHI_SetFreq(0,17640,actrl,AHISF_IMM);
  141.                       AHI_SetVol(0,0x10000L,0x8000L,actrl,AHISF_IMM);
  142.                       AHI_SetSound(0,0,0,0,actrl,AHISF_IMM);
  143. */
  144.                       for(;;)
  145.                       {
  146.                         signals=Wait((1L<<signal) | SIGBREAKF_CTRL_C);
  147.                         if(signals & SIGBREAKF_CTRL_C)
  148.                           break;
  149.  
  150.                         if(DBflag)
  151.                         {
  152.                           readsamples=Read(Input(),Sample1.ahisi_Address,BufferLen*2)/2;
  153.                           if(readsamples<BufferLen)
  154.                           {
  155.                             // Clear rest of buffer
  156.                             for(i=readsamples;i<BufferLen;i++)
  157.                               ((WORD *)Sample1.ahisi_Address)[i]=0;
  158.                             Wait(1L<<signal);
  159.                             // Clear other buffer
  160.                             for(i=0;i<BufferLen;i++)
  161.                               ((WORD *)Sample0.ahisi_Address)[i]=0;
  162.                             break;
  163.                           }
  164.                         }
  165.                         else
  166.                         {
  167.                           readsamples=Read(Input(),Sample0.ahisi_Address,BufferLen*2)/2;
  168.                           if(readsamples<BufferLen)
  169.                           {
  170.                             // Clear rest of buffer
  171.                             for(i=readsamples;i<BufferLen;i++)
  172.                               ((WORD *)Sample0.ahisi_Address)[i]=0;
  173.                             Wait(1L<<signal);
  174.                             // Clear other buffer
  175.                             for(i=0;i<BufferLen;i++)
  176.                               ((WORD *)Sample1.ahisi_Address)[i]=0;
  177.                             break;
  178.                           }
  179.                         }
  180.                         
  181.  
  182.                       }
  183.                       if(signals & SIGBREAKF_CTRL_C)
  184.                         Printf("***Break\n");
  185.                       else
  186.                         Wait(1L<<signal);   // Wait for half-loaded buffer to finish.
  187.  
  188.                       AHI_ControlAudio(actrl,
  189.                           AHIC_Play,FALSE,
  190.                           TAG_DONE);
  191.                     }
  192.                     FreeSignal(signal);
  193.                     signal=-1;
  194.                   }
  195.                   
  196.                 }
  197.                 else
  198.                   Printf("Cannot intialize sample buffers\n");
  199.               }
  200.               else
  201.                 Printf("Out of memory.\n");
  202.  
  203.               FreeVec(Sample0.ahisi_Address);
  204.               FreeVec(Sample1.ahisi_Address);
  205.               Sample0.ahisi_Address=NULL;
  206.               Sample1.ahisi_Address=NULL;
  207.               AHI_FreeAudio(actrl);
  208.               actrl=NULL;
  209.             }
  210.             else
  211.               Printf("Unable to allocate sound hardware\n");
  212.           }
  213.           else
  214.           {
  215.             if(IoErr() EQ NULL)
  216.               Printf("Aborted.\n");
  217.             else if(IoErr() EQ ERROR_NO_MORE_ENTRIES)
  218.               Printf("No available audio modes.\n");
  219.             else if(IoErr() EQ ERROR_NO_FREE_STORE)
  220.               Printf("Out of memory.\n");
  221.           }
  222.           
  223.           AHI_FreeAudioRequest(req);
  224.           req=NULL;
  225.         }
  226.         else
  227.           Printf("Out of memory.\n");
  228.  
  229.         CloseDevice((struct IORequest *)AHIio);
  230.         AHIDevice=-1; // Good habit, IMHO.
  231.       }
  232.       DeleteIORequest((struct IORequest *)AHIio);
  233.       AHIio=NULL;
  234.     }
  235.     DeleteMsgPort(AHImp);
  236.     AHImp=NULL;
  237.   }
  238. }
  239.